package com.corner.presenter.imp;

import com.alibaba.fastjson.JSONObject;
import com.avos.avoscloud.AVInstallation;
import com.avos.avoscloud.AVPush;
import com.avos.avoscloud.AVQuery;
import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.domain.Like;
import com.corner.model.cornerservice.domain.ReportInfo;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.cornerservice.helper.InfoHelper;
import com.corner.model.cornerservice.service.Service;
import com.corner.model.cornerservice.service.exception.BaseException;
import com.corner.presenter.IInfoBasePresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.adapter.InfoAdapter;
import com.corner.ui.view.IInfoBaseView;
import com.corner.utils.NetUtil;

import java.util.HashMap;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/14，15.37.
 * 描述：
 */
public class InfoBasePresenter extends BasePresenter<IInfoBaseView> implements IInfoBasePresenter {

    @Override
    public void requestReadedCount(final InfoAdapter.Base base, final Info info) {
        InfoHelper.getReadedUserCount(info).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long integer) {
                if (view != null) {
                    info.watchNum = integer.toString();
                    view.showReadedUserCount(base, info);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                info.hasRequestReadedCount = false;
            }
        });
    }

    @Override
    public void requestAgreeCount(final InfoAdapter.Base base, final Info info) {
        InfoHelper.getLikeUserCount(info).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long integer) {
                if (view != null) {
                    info.agreeNum = integer.toString();
                    view.showAgreeUserCount(base, info);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                info.hasRequestReadedCount = false;
            }
        });
    }

    @Override
    public void requestIsLike(final InfoAdapter.Base base, final Info info) {
        InfoHelper.getLike(info, User.getCurrentUser()).observeOn(AndroidSchedulers.mainThread()).doOnNext(new Action1<Like>() {
            @Override
            public void call(Like like) {
                if (like == null) {
                    InfoHelper.readOrLikeOrNotSee(info, User.getCurrentUser(), Like.READED).subscribe();
                }
            }
        }).subscribe(new Action1<Like>() {
            @Override
            public void call(Like like) {
                if (like != null) {
                    info.isLike = like.getType() == Like.LIKED;
                } else {
                    info.isLike = false;
                }
                if (view != null)
                    view.setIsLike(base, info);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                info.hasRequestIsLike = false;
            }
        });
    }

    @Override
    public void requestLike(Info info, boolean isLike) {
        info.isLike = isLike;
        int type;
        if (isLike) {
            type = Like.LIKED;
        } else {
            type = Like.READED;
        }
        InfoHelper.readOrLikeOrNotSee(info, (User.getCurrentUser()), type).observeOn(AndroidSchedulers.mainThread()).subscribe();
        if (isLike) {
            if (info != null && info.getUser() != null && (info.getUser()).getInstallationId() != null) {
                AVQuery pushQuery = AVInstallation.getQuery();
                pushQuery.whereEqualTo("installationId", (info.getUser()).getInstallationId());
                HashMap<String, Object> map = new HashMap<>();
                map.put(ConsTants.NOTIFY_STR_ACTION, ConsTants.NOTIFY_ACTION_LIKE);
                map.put(ConsTants.NOTIFY_INFO_ID, info.getObjectId());
                map.put(ConsTants.NOTIFY_USER_ID, User.getCurrentUser().getObjectId());
                map.put(ConsTants.NOTIFY_TIME, System.currentTimeMillis() + "");
                JSONObject jsonObject = new JSONObject(map);
                AVPush avPush = new AVPush();
                avPush.setQuery(pushQuery);
                avPush.setData(jsonObject);
                avPush.sendInBackground();
            }
        }
    }

    @Override
    public void deleteInfo(Info info) {
        if (!NetUtil.checkNet(App.application)) {
            view.showLoading(false);
            return;
        }
        view.showLoading(true);
        InfoHelper.deleteInfo(info).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Info>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.deleteError(e.getMessage());
                    view.showLoading(false);
                }
            }

            @Override
            public void onNext(Info info) {
                if (view != null) {
                    view.deleteSucess(info);
                    view.showLoading(false);
                }
            }
        });

    }

    @Override
    public void noWantToSee(final Info info) {
        view.showLoading(true);
        InfoHelper.readOrLikeOrNotSee(info, User.getCurrentUser(), Like.NOT_SEE).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Like>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.showLoading(false);
                }
            }

            @Override
            public void onNext(Like like) {
                if (view != null) {
                    view.noWantToSeeSucess(like.getInfo());
                    view.showLoading(false);
                }
            }
        });
    }

    @Override
    public void report(final Info info) {
        Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                ReportInfo report = new ReportInfo();
                report.setInfo(info);
                report.setCreateUser(User.getCurrentUser());
                try {
                    Service.getInstance().reportInfoService.save(report);
                } catch (BaseException e) {
                    e.printStackTrace();
                    subscriber.onError(new Throwable(App.application.getString(R.string.request_data_error)));
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {
            @Override
            public void call(Object o) {
                if (view != null) {
                    view.showReportSucess();
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                if (view != null) {
                    view.showReportError(throwable.getMessage());
                }
            }
        });
    }
}
